package io.renren.modules.web.controller;

import io.renren.common.utils.R;
import io.renren.modules.web.entity.UserEntity;
import io.renren.modules.web.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 学生注册
 */
@Api("用户注册")
@RequestMapping("web")
@RestController
public class AppSignInController {

    @Autowired
    private UserService userService;
    
    @PostMapping("/signIn")
    @ApiOperation("用户注册")
    public R signIn(@RequestBody UserEntity userEntity){
        //查询是否存在
        String mobile = userEntity.getMobile();
        userEntity.setCreateTime(new Date());
        UserEntity oldUser = userService.queryByMobile(mobile);
        if (oldUser!=null ){
            return R.error(5001,"手机号已被注册");
        }else {
            userEntity.setPassword(DigestUtils.sha256Hex(userEntity.getPassword()));
            userEntity.setType(1);
            userService.save(userEntity);
        }
        return new R();
    }
}
